package cn.cnsy123.shorturls.api.controller.page;

import java.net.URLEncoder;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.cnsy123.shorturls.api.controller.vo.ErrorResultVo;
import cn.cnsy123.shorturls.constants.SessionInfoConstants;

@Controller
public class LoginPageController {
    
    @RequestMapping(value = "/loginPage",method = RequestMethod.GET)
    public ModelAndView skipLoginPage(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setStatus(HttpStatus.OK);
        Object sessionUserInfo = request.getSession().getAttribute(SessionInfoConstants.SESSION_USER_BEAN);
        if(sessionUserInfo != null) {
            modelAndView.setViewName("redirect:/");
        }else {
            modelAndView.setViewName("loginPage");
        }
        
        return modelAndView;
    }
    
    @RequestMapping(value = "/",method = RequestMethod.GET)
    public ModelAndView skipHomeLoginPage(HttpServletRequest request) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setStatus(HttpStatus.OK);
        modelAndView.setViewName("mainPage");
        return modelAndView;
    }
    
    
    @RequestMapping(value = "/error/404",produces = MediaType.TEXT_HTML_VALUE)
    public ModelAndView skip404ErrorPage(HttpServletRequest request,HttpServletResponse response) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setStatus(HttpStatus.OK);
        modelAndView.setViewName("error/404");
        return modelAndView;
    }
    
    @RequestMapping(value = "/error/500",produces = MediaType.TEXT_HTML_VALUE)
    public ModelAndView skip500ErrorPage(HttpServletRequest request,HttpServletResponse response) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setStatus(HttpStatus.OK);
        modelAndView.setViewName("error/500");
        return modelAndView;
    }
    
    @RequestMapping(value = "/error/404")
    @ResponseBody
    public ErrorResultVo skip404ErrorPage() {
        return new ErrorResultVo(404, "没有对应的视图", HttpStatus.NOT_FOUND.value(), new Date(System.currentTimeMillis()));
    }
    
    @RequestMapping(value = "/error/500")
    @ResponseBody
    public ErrorResultVo skip500ErrorPage() {
        return new ErrorResultVo(500, "请求异常", HttpStatus.INTERNAL_SERVER_ERROR.value(), new Date(System.currentTimeMillis()));
    }
    
    
    
    @RequestMapping(value = "/logout")
    public ModelAndView logout(HttpServletRequest request) throws Exception {
        request.getSession().removeAttribute(SessionInfoConstants.SESSION_USER_BEAN);
        request.getSession().invalidate();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setStatus(HttpStatus.OK);
        modelAndView.setViewName("redirect:/loginPage?errorMsg="+URLEncoder.encode("用户主动退出系统","UTF-8"));
        return modelAndView;
    }
    

}
